/**
 * 更改登录窗口大小
 */
function changeHight() {
	var vh = $(window).height();
    $(".index_login").height(vh);
}

/**
 * 设置登录状态
 */
function setLoginStatus() {
	$(".re_password .rem_btn").click(function(){
        if ($(this).find("img").css("margin-top") == "0px") {
            $(this).find("img").css("margin-top", "-30px");
            $('#rememberMe').val('false');
        }else{
            $(this).find("img").css("margin-top", "0px");
            $('#rememberMe').val('true');
        }
    });
}

/**
 * 数据验证
 */
function validateLoginForm(){
	$("#loginForm").validate({
        rules: {
        	userName: {
                required: true,
                maxlength:30
            },
            password: {
                required: true,
                maxlength:30
            }
        },
        errorPlacement: function(error, element) {
            error.appendTo(element.parent());
        },
        errorElement: "span",
        errorClass: "errortips",
        success: 'valid',
        messages: {
        	userName: {
                required: "(请输入用户名)",
                maxlength:"(最多输入{0}个字符)"
            },
            password: {
                required: "(请输入密码)",
                maxlength:"(最多输入{0}个字符)"
            }
        }
    });
}

/**
 * 登录成功后处理-存储用户登录信息
 * @param {Object} userName
 * @param {Object} uuid
 */
function loginSuccessDeal(data,userCode,pwd){
	var storage = window.localStorage;
	if(storage){
		storage.setItem($$.sysFlag+"hgtg-token",data.token);
		storage.setItem($$.sysFlag+"hgtg-userName",data.userName);
		var rememberMe = $("#rememberMe").val();
		if(rememberMe=="true"){
			storage.setItem($$.sysFlag+"hgtg-userCode",userCode);
		}
	}else{
		$$.alert("浏览器版本过低!");
	}
}

function selectTenant(appCode,tenantCodes){
	var openWin;
    layer.open({
        move: false,
        type: 2,
        title: "选择租户",
        area: ['600px', '300px'],
        content: "select_tenant.html",
        btn: ['确定','关闭'],
        btnAlign: 'c',
        success: function (layero, index) {
            openWin = window[layero.find('iframe')[0]['name']];
            openWin.initData(appCode,tenantCodes);
        },
        yes: function (index, layero) {
        	var selectData = openWin.getSelectData();
        	if(selectData){
        		layer.close(index);
        		loginSystem(selectData.tenantCode);
        	}
        },
        cancel: function (index) {
            layer.close(index);
        }
    });
}

/**
 * 执行登录
 */
function loginSystem(tenantCode){
	if($('#loginForm').valid()) {
		var appCode = $("#appCode").val();
		var userCode = $("#userName").val();
		var pwd = $("#password").val();
		$$.ajax({
	        url:"/rest/BaseAuthBusiness/login",
	        suiteCode:"base",
	        data : {
	            "appCode": appCode,
	            "tenantCode": tenantCode,
	        	"userCode": userCode,
	            "password": pwd
	        },
	        success : function(data){
	        	if(data.result=="success"){
	        		loginSuccessDeal(data,userCode,pwd);
		            var firstPageUrl = data.firstPageUrl;
		            if(!firstPageUrl || firstPageUrl===""){
		            	firstPageUrl = "/base/index/default.html";
		            }
		            $$.goUrl(firstPageUrl);
	        	}else if(data.result=="manyTenant"){
	        		selectTenant(data.appCode,data.tenantCodes);
	        	}
	        }
	    });
    }
}

$(function () {
    changeHight();
    setLoginStatus();
    validateLoginForm();
    
    //设置登录信息
    var storage = window.localStorage;
	if(storage){
		var userCode = storage.getItem($$.sysFlag+"hgtg-userCode");
		if(userCode){
			$("#userName").val(userCode);
		}
	}
    
    //提交登录
    $("#subBtn").on('click',function () {
        loginSystem("");
    });
    
    $('.entrer-login').bind('keypress',function(e){
        if(e.keyCode == 13){
        	loginSystem("");
        }
    });
    
    $(window).resize(function () {
	    changeHight();
	});
});
